package edu.uw.nemo.esu;

import org.apache.log4j.Logger;

import java.io.Serializable;
import java.util.*;

/**
 * generate motifs of given length with ESU algorithm
 * single threaded implementation
 *
 */
public class ESUGen implements Serializable {

    private static Logger logger = Logger.getLogger(ESUGen.class);

    public List<int[]> enumerateSubgraphs(Map<Integer, Set<Integer>> adj, List<Integer> vertices, int length) {
        long start = System.currentTimeMillis();
        EnumerationCollector result = new EnumerationCollector();
        for (Integer node : vertices) {
            List<Integer> subGraph = new ArrayList<Integer>();
            subGraph.add(node);
            List<Integer> extension = filterGreater(getNeighbours(adj, node), node);
            extendSubgraph(adj, subGraph, extension, length, result);
        }
        logger.debug("******************************************************** enumerate subgraph took " + (System.currentTimeMillis() - start) + " milliseconds.");

        return result.getMotifs();
    }

    List<Integer> filterGreater(List<Integer> neighbours, Integer node) {
        ArrayList<Integer> filetered = new ArrayList<Integer>();
        for(int i : neighbours) {
            if (i > node) {
                filetered.add(i);
            }
        }
        return filetered;
    }

    void extendSubgraph(Map<Integer, Set<Integer>> adj, List<Integer> subGraph, List<Integer> extension, int motifLength, Collector collector) {
        int size = subGraph.size();
        if (size >= motifLength) {
            int[] x = new int[motifLength];
            for (int i = 0; i < motifLength; i++) {
                x[i] = subGraph.get(i);
            }
//            System.out.println(x);
            collector.add(x);
        } else {
//            System.out.print("sub graph = "); print(subGraph); System.out.print(", ");
//            System.out.print("extension = "); print(extension); System.out.println("");
            Integer root = subGraph.get(0);
            while(extension.size() > 0) {
                int vertex = extension.get(0);
                extension.remove(0);
                Set<Integer> nExt = new HashSet<Integer>(extension);
                nExt.addAll(filterGreater(getExclusiveNeighbours(adj, subGraph, vertex), root));
                nExt.remove(vertex);
                subGraph.add(vertex);
                extendSubgraph(adj, subGraph, new ArrayList<Integer>(nExt), motifLength, collector);
                subGraph.remove(subGraph.size() - 1);
            }
        }
    }

    private List<Integer> getExclusiveNeighbours(Map<Integer, Set<Integer>> adjMapping, List<Integer> subGraph, int node) {
        Set<Integer> old = new HashSet<Integer>();
        for (Integer x : subGraph) {
            old.addAll(getNeighbours(adjMapping, x));
        }
        Set<Integer> n = new HashSet<Integer>(getNeighbours(adjMapping, node));
        n.removeAll(old);
        return new ArrayList<Integer>(n);
    }

    List<Integer> getNeighbours(Map<Integer, Set<Integer>> adjMapping, int node) {
        return new ArrayList<Integer>(adjMapping.get(node));
    }

}
